package cn.rxyb.core.io.file;

import cn.rxyb.core.io.IoUtil;
import cn.rxyb.core.io.enums.LineSeparator;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author YJH
 * @version 1.0
 * @description 文件写入
 * @date 2020/6/28 16:50
 */
public class FileWrite extends FileWrapper {

    public FileWrite(File file) {
        super(file);
    }

    public FileWrite(File file, Charset charset) {
        super(file, charset);
    }

    public FileWrite(String filePath) {
        super(filePath);
    }

    public FileWrite(String filePath, Charset charset) {
        super(filePath, charset);
    }


    /**
     * 文件写入
     *
     * @param content 内容
     */
    public void write(String content) {
        Write.write(content, false, file, charset);
    }

    /**
     * 内容追加
     *
     * @param content 追加内容
     * @param isLine  是否换行
     */
    public void append(String content, boolean isLine) {
        if (isLine) {
            PrintWriter printWriter = null;
            try {
                printWriter = new PrintWriter(Write.getBufferedWriter(file, true, charset));
                Write.printNewLine(printWriter, LineSeparator.LINUX);
                printWriter.write(content);
                printWriter.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                IoUtil.close(printWriter);
            }
        } else {
            Write.write(content, true, file, charset);
        }

    }

    /**
     * 批量写入内容
     *
     * @param content
     */
    public void writeList(List<String> content) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(Write.getBufferedWriter(file, false, charset));
            for (String item : content) {
                printWriter.write(item);
                Write.printNewLine(printWriter, null);
                printWriter.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(printWriter);
        }
    }

    /**
     * 批量写入内容
     *
     * @param content       内容
     * @param lineSeparator 当前操作系统
     */
    public void writeList(List<String> content, LineSeparator lineSeparator) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(Write.getBufferedWriter(file, false, charset));
            for (String item : content) {
                printWriter.write(item);
                Write.printNewLine(printWriter, lineSeparator);
                printWriter.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(printWriter);
        }
    }

    /**
     * 内容追加
     *
     * @param content       内容
     * @param lineSeparator 当前操作系统
     */
    public void appendList(List<String> content, LineSeparator lineSeparator) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(Write.getBufferedWriter(file, false, charset));
            for (String item : content) {
                Write.printNewLine(printWriter, lineSeparator);
                printWriter.write(item);
                printWriter.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(printWriter);
        }
    }


    /**
     * 批量追加内容
     *
     * @param content
     */
    public void appendList(List<String> content) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(Write.getBufferedWriter(file, true, charset));
            for (String item : content) {
                Write.printNewLine(printWriter, null);
                printWriter.write(item);
                printWriter.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(printWriter);
        }
    }


    static class Write {

        /**
         * 获取文件流信息
         *
         * @param file     文件名称
         * @param isAppend 是否追加
         * @param charset  编码字符集
         * @return
         * @throws FileNotFoundException
         */
        public static BufferedWriter getBufferedWriter(File file, boolean isAppend, Charset charset) throws FileNotFoundException {
            if (null == charset) {
                return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, isAppend)));
            }
            return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, isAppend), charset));
        }

        /**
         * 文件写入
         *
         * @param content  内容
         * @param isAppend 是否追加内容
         * @param file     文件
         * @param charset  字符编号
         */
        public static void write(String content, boolean isAppend, File file, Charset charset) {
            BufferedWriter bufferedWriter = null;
            try {
                if (null == charset) {
                    bufferedWriter = getBufferedWriter(file, isAppend, Charset.forName("UTF-8"));
                } else {
                    bufferedWriter = getBufferedWriter(file, isAppend, charset);
                }
                bufferedWriter.write(content);
                bufferedWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IoUtil.close(bufferedWriter);
            }
        }

        /**
         * 打印新行
         *
         * @param writer        Writer
         * @param lineSeparator 换行符枚举
         * @since 4.0.5
         */
        public static void printNewLine(PrintWriter writer, LineSeparator lineSeparator) {
            if (null == lineSeparator) {
                //默认换行符
                writer.println();
            } else {
                //自定义换行符
                writer.print(lineSeparator.getValue());
            }
        }
    }
}
